#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
using LL = long long;
const int N = 1e5 + 10, M = 3e5 + 10, INF = 0x3f3f3f3f;

struct  Edge
{
    int a, b, w;
    bool used;

    bool operator< (const Edge& W)const{
        return w < W.w;
    }
}edge[M];


int n, m;
int q[N];
int depth[N], fa[N][17], d1[N][17], d2[N][17];
int p[N];
int h[N], e[M], ne[M], w[M], idx;

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }
    return p[x];
}

void add(int a, int b, int c){
    e[idx] = b;
    ne[idx] = h[a];
    w[idx] = c;
    h[a] = idx ++;
}

LL kruskal(){
    memset(h, -1, sizeof h);
    for(int i = 1; i <= n; i ++) p[i] = i;
    sort(edge, edge + m);

    LL res = 0;
    for(int i = 0; i < m; i ++){
        int x = edge[i].a;
        int y = edge[i].b;
        int z = edge[i].w;
        int a = find(x), b = find(y);
        if(a != b){
            p[a] = p[b];
            res = (LL) res + z;
            edge[i].used = true;
            add(x, y, z), add(y, x, z);
        }
    }

    return res;
}


void bfs(){
    memset(depth, 0x3f, sizeof depth);
    depth[0] = 0, depth[1] = 1;
    int hh = 0, tt = 0;
    
    q[tt++] = 1;

    while(hh != tt){
        int t = q[hh ++];
        if(hh == N) hh = 0;

        for(int i = h[t]; ~i; i = ne[i]){
            int j = e[j];
            if(depth[j] > depth[t] + 1){
                depth[j] = depth[t] + 1;
                q[tt++] = j;
                if(tt == N) tt = 0;

                fa[j][0] = t;
                d1[j][0] = w[i], d2[j][0] = -INF;

                for(int k = 1; k <= 16; k ++){
                    int anc = fa[j][k - 1];
                    fa[j][k] = fa[anc][k - 1];

                    int distance[4] = {d1[j][k - 1], d2[j][k - 1], d1[anc][k - 1], d2[anc][k - 1]};
                    d1[j][k] = d2[j][k] = -INF;
                    for(int u = 0; u < 4; u ++){
                        int d = distance[u];
                        if(d > d1[j][k]){
                            d2[j][k] = d1[j][k];
                            d1[j][k] = d;
                        }else if(d != d1[j][k] && d > d2[j][k]){
                            d2[j][k] = d;
                        }
                    }
                } 

            }
        }
    }

}

int lca(int a, int b, int w){
    static int distance[N * 2];
    int cnt = 0;

    if(depth[a] < depth[b]) swap(a, b);

    for(int k = 16; k >= 0; k --){
        if(depth[fa[a][k]] >= depth[b]){
            distance[cnt ++] = d1[a][k];
            distance[cnt ++] = d2[a][k];
            a = fa[a][k];
        }
    }

    if(a != b){
        for(int k = 16; k >= 0; k --){
            if(fa[a][k] != fa[b][k]){
                distance[cnt ++] = d1[a][k];
                distance[cnt ++] = d2[a][k];
                distance[cnt ++] = d1[b][k];
                distance[cnt ++] = d2[b][k];

                a = fa[a][k];
                b = fa[b][k];
            }
        }

        distance[cnt ++] = d1[a][0];
        distance[cnt ++] = d1[b][0];
    }


    int dist1 = -INF, dist2 = -INF;
    for(int i = 0; i < cnt; i ++){
        if(distance[i] > dist1){
            dist2 = dist1;
            dist1 = distance[i];
        }else if(distance[i] != dist1 && distance[i] > dist2){
            dist2 = distance[i];
        }
    }

    if(w > dist1) return w - dist1;
    if(w > dist2) return w - dist2;
    return INF;
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    
    scanf("%d%d", &n, &m);

    for(int i = 0; i < m; i ++){
        int a, b, w;
        scanf("%d%d%d", &a, &b, &w);
        edge[i].a = a;
        edge[i].b = b;
        edge[i].w = w;
        edge[i].used = false;
    }

    LL sum = kruskal();
    bfs();

    LL res = 1e18;
    for(int i = 0; i < m; i ++){
        if(!edge[i].used){
            int a = edge[i].a;
            int b = edge[i].b;
            int w = edge[i].w;

            res = min(res, (LL)sum + lca(a, b, w));
        }
    }

    printf("%lld", res);
    
    return 0;

}